import apiClient from './client'

export interface DashboardData {
  totalOrders: number
  totalGmv: number
  totalUsers: number
  totalProducts: number
  todayOrders: number
  todayGmv: number
  yesterdayOrders: number
  yesterdayGmv: number
  yesterdayNewUsers: number
  yesterdayNewProducts: number
  orderChange: number
  revenueChange: number
  userChange: number
  productChange: number
}

export interface SalesTrendData {
  dates: string[]
  orderCounts: number[]
  dailyGmv: number[]
}

export interface BrandPerformanceData {
  brandNames: string[]
  productCounts: number[]
  totalSales: number[]
  totalRevenue: number[]
}

export interface UserStatisticsData {
  totalUsers: number
  newUsers: number
  newUsers30: number
  dates: string[]
  dailyNewUsers: number[]
}

export interface OrderStatusData {
  statusNames: string[]
  counts: number[]
}

// 新增的数据类型定义
export interface SalesFunnelData {
  funnelData: Array<{
    name: string
    value: number
    count: number
  }>
}

export interface TopProductsData {
  topProducts: Array<{
    rank: number
    name: string
    price: number
    salesVolume: number
    revenue: number
  }>
}

export interface PriceRangeData {
  ranges: string[]
  counts: number[]
}

export interface InventoryStatusData {
  inStock: number
  lowStock: number
  outOfStock: number
}

export interface TimeDistributionData {
  hours: string[]
  counts: number[]
}

export interface RegionDistributionData {
  provinces: string[]
  counts: number[]
}

export const reportService = {
  // 获取仪表板概览数据
  async getDashboardData(): Promise<DashboardData> {
    const response = await apiClient.get<DashboardData>('/api/reports/dashboard')
    return response.data.data
  },

  // 获取销售趋势数据
  async getSalesTrend(): Promise<SalesTrendData> {
    const response = await apiClient.get<SalesTrendData>('/api/reports/sales/trend')
    return response.data.data
  },

  // 获取品牌表现数据
  async getBrandPerformance(): Promise<BrandPerformanceData> {
    const response = await apiClient.get<BrandPerformanceData>('/api/reports/brand/performance')
    return response.data.data
  },

  // 获取用户统计数据
  async getUserStatistics(): Promise<UserStatisticsData> {
    const response = await apiClient.get<UserStatisticsData>('/api/reports/user/statistics')
    return response.data.data
  },

  // 获取订单状态分布
  async getOrderStatusDistribution(): Promise<OrderStatusData> {
    const response = await apiClient.get<OrderStatusData>('/api/reports/order/status')
    return response.data.data
  },

  // 新增的报表接口
  // 获取销售转化漏斗
  async getSalesFunnel(): Promise<SalesFunnelData> {
    const response = await apiClient.get<SalesFunnelData>('/api/reports/sales/funnel')
    return response.data.data
  },

  // 获取商品销售排行榜
  async getTopProducts(): Promise<TopProductsData> {
    const response = await apiClient.get<TopProductsData>('/api/reports/products/top')
    return response.data.data
  },

  // 获取价格区间分布
  async getPriceRangeDistribution(): Promise<PriceRangeData> {
    const response = await apiClient.get<PriceRangeData>('/api/reports/price/range')
    return response.data.data
  },

  // 获取库存状态统计
  async getInventoryStatus(): Promise<InventoryStatusData> {
    const response = await apiClient.get<InventoryStatusData>('/api/reports/inventory/status')
    return response.data.data
  },

  // 获取订单时间分布
  async getOrderTimeDistribution(): Promise<TimeDistributionData> {
    const response = await apiClient.get<TimeDistributionData>('/api/reports/order/time')
    return response.data.data
  },

  // 获取订单地区分布
  async getOrderRegionDistribution(): Promise<RegionDistributionData> {
    const response = await apiClient.get<RegionDistributionData>('/api/reports/order/region')
    return response.data.data
  }
}
